unit IEmode;

interface

type
  TIEMode = (iemUnknown, iemIE7, iemIE8, iemIE9, iemIE10);
  // iemUnknown, don't use this as parameter, return result only
  // iemIE10: To run a WebBrowser control in IE10 Standards Mode
  // iemIE9: To run a WebBrowser control in IE9 Standards Mode
  // iemIE8: To run a WebBrowser control in IE8 Standards Mode
  // iemIE7: To run in IE7 Standards Mode

function setEmbeddedWebbrowserMode(Mode: TIEMode; AppName: string = ''): boolean;
function getEmbeddedWebbrowserMode(AppName: string = ''): TIEMode;

implementation

uses
  Windows, Registry, SysUtils, Forms;

function embeddedWebbrowserMode(bSet: boolean; Mode: TIEMode; AppName: string = ''): LongInt;
const
  REG_KEY = 'Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION';
var
  Reg: TRegistry;
  Value: LongInt;
  i: LongInt;
begin
  Result := 0;
  if (bSet) and (Mode = iemUnknown) then
    Exit;

  if AppName = '' then
    AppName := ExtractFileName(Application.ExeName);

  if (bSet) then
    case Mode of
      iemIE7:
        Value := 7000;
      iemIE8:
        Value := 8888;
      iemIE9:
        Value := 9999;
    else
      Value := 10001; // IE10 standards mode
    end
  else
    Value := 0;

  Reg := nil;
  try
    Reg := TRegistry.Create();
    Reg.RootKey := HKEY_CURRENT_USER;
    if (Reg.OpenKey(REG_KEY, True)) then
    begin
      if (bSet) then
      begin
        Reg.WriteInteger(AppName, Value);
        Result := Value;
      end
      else
        Value := Reg.ReadInteger(AppName);
      Reg.CloseKey;
    end;
  except
    ;
  end;

  if (Assigned(Reg)) then
    FreeAndNil(Reg);

  if (NOT bSet) and (Value > 0) then
  begin
    i := Value div 1000;
    if (i >= 7) and (i <= 10) then
    begin
      case i of
        7000:
          Result := Byte(iemIE7);
        8888:
          Result := Byte(iemIE8);
        9999:
          Result := Byte(iemIE9);
        10001:
          Result := Byte(iemIE10);
      else
        begin
          if (i >= 10) then
            Result := Byte(iemIE10);
        end;
      end;
    end;
  end;
end;

function setEmbeddedWebbrowserMode(Mode: TIEMode; AppName: string): boolean;
begin
  Result := (embeddedWebbrowserMode(True, Mode, AppName) > 0);
end;

function getEmbeddedWebbrowserMode(AppName: string): TIEMode;
begin
  Result := TIEMode(Byte(embeddedWebbrowserMode(FALSE, iemUnknown, AppName)));
end;

end.
